package org.cuatrovientos.onceuponatime;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Dani
 * Servlet implementation class Update
 */
@WebServlet("/Update")
public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String title="";
		String tale="";
		String[] fulltale;
		try {
			fulltale = getTale(id);
			title = fulltale[0];
			tale = fulltale[1];
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("id", id);
		request.setAttribute("title", title);
		request.setAttribute("tale", tale);
		this.getServletContext().getRequestDispatcher("/update.jsp")
		.forward(request, response);
	}

	private String[] getTale(String id) throws SQLException {
		String tale[] = {"", ""};
		String urlcon= "jdbc:mysql://localhost:3306/onceupon";
		ResultSet resultset = null;
				Connection conn = null;
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(urlcon, "root", "");
					Statement stat = conn.createStatement();
				
				resultset = stat.executeQuery("SELECT tale from tale WHERE id ="+id);
				resultset.next();
				tale[0]= resultset.getString("title");
				tale[1] = resultset.getString("tale");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			conn.close();
				return tale;
	}

	private String getTitle(String id) throws SQLException {
		String title="";
String urlcon= "jdbc:mysql://localhost:3306/onceupon";
ResultSet resultset = null;
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(urlcon, "root", "");
			Statement stat = conn.createStatement();
		
		resultset = stat.executeQuery("SELECT title from tale WHERE id ="+id);
		resultset.next();
		title = resultset.getString("title");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	conn.close();
		return title;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String tale = request.getParameter("tale");
		try {
			updateTale(id, title, tale);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.getServletContext().getRequestDispatcher("/Home")
		.forward(request, response);
	}

	/**
	 * Edits an existing tale of the database
	 * @param id
	 * @param title
	 * @param tale
	 * @throws SQLException
	 */
	private void updateTale(String id, String title, String tale) throws SQLException {
String urlcon= "jdbc:mysql://localhost:3306/onceupon";
		
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(urlcon, "root", "");
			Statement stat = conn.createStatement();
		stat.execute("update tale set title= '"+title+"', tale='"+tale+"' where id ="+id);
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	conn.close();
		
		
	}

}
